package cn.cheqi.recovery.api.controller;


import cn.cheqi.recovery.api.dto.DataGridView;
import cn.cheqi.recovery.api.entity.Owner;
import cn.cheqi.recovery.api.service.OwnerService;
import cn.cheqi.recovery.api.vo.OwnerVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  api接口未入驻业主前端控制器
 * </p>
 *
 * @author Robin
 * @since 2020-02-23
 */
@RestController("apiOwnerController")
@RequestMapping("/api/owner")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    /**
     * api接口展示所有未入驻业主列表
     * @param ownerVO
     * @return
     */
    @RequestMapping("/loadAllOwners")
    public DataGridView loadAllOwners(OwnerVO ownerVO){
        IPage<Owner> page = new Page<>(ownerVO.getPage(),ownerVO.getLimit());
        QueryWrapper<Owner> wrapper = new QueryWrapper<>();
        //获取查询体内的业主ID
        Long ownerId = ownerVO.getOwnerId();
        //获取查询体内的业主姓名
        String ownerName = ownerVO.getOwnerName();
        //获取查询体内的业主联系方式
        String ownerPhone = ownerVO.getOwnerPhone();
        //判断这三个条件是否为空,若不是则加进查询条件
        if (ownerId != null){
            wrapper.like("owner_id",ownerId);
        }
        //模糊查询业主姓名
        if (!StringUtils.isBlank(ownerName)){
            //去掉字符串中的空格
            String nameTrim = ownerName.replace(" ", "");
            wrapper.like(nameTrim != null,"owner_name",nameTrim);
        }
        //模糊查询业主联系方式
        if (!StringUtils.isBlank(ownerPhone)){
            //去掉字符串中的空格
            String phoneTrim = ownerPhone.replace(" ", "");
            wrapper.like(phoneTrim != null,"owner_phone",phoneTrim);
        }
        //通过id列倒序输出sql结果
        wrapper.orderByDesc("owner_id");
        ownerService.page(page,wrapper);
        //DTO传输对象
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCode(0);
        dataGridView.setMsg("");
        Map<Object,Object> map = new HashMap<>();
        map.put("count",page.getRecords().size());
        map.put("page",page.getCurrent());
        map.put("zpage",page.getPages());
        map.put("list",page.getRecords());
        dataGridView.setData(map);
        return dataGridView;
    }

    /**
     * 通过未入驻业主ID获取业主信息
     * @param ownerVO
     * @return
     */
    public DataGridView getOwnerById(OwnerVO ownerVO){
        Owner owner = ownerService.getById(ownerVO.getOwnerId());
        if (owner != null){
            return new DataGridView(0,"",owner);
        }else {
            return new DataGridView(1,"您获取的信息不存在","");
        }
    }


    /**
     * api接口添加未入驻业主
     * @param ownerVO
     * @return
     */
    @PostMapping("/addOwner")
    @Transactional
    public DataGridView addOwner(OwnerVO ownerVO){
        QueryWrapper<Owner> wrapper = new QueryWrapper<>();
        wrapper.eq("owner_phone",ownerVO.getOwnerPhone());
        //若>0,则手机号已存在
        if (ownerService.count(wrapper) > 0){
            return new DataGridView(1,"该手机号已存在",null);
        }
        try {
            ownerService.save(ownerVO);
            return new DataGridView(0,"添加成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGridView(1,"添加失败",null);
        }
    }

    /**
     * api接口根据未入驻业主ID删除业主
     * @param ownerVO
     * @return
     */
    @PostMapping("/deleteOwnerById")
    @Transactional
    public DataGridView deleteOwnerById(OwnerVO ownerVO){
        if (ownerService.getById(ownerVO.getOwnerId()) == null){
            return new DataGridView(1,"您想要删除的未入驻业主信息不存在","");
        }
        try {
            ownerService.removeById(ownerVO.getOwnerId());
            return new DataGridView(0,"删除成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGridView(1,"删除失败",null);
        }
    }

    /**
     * api接口通过未入驻业主ID编辑信息
     * @param ownerVO
     * @return
     */
    @PostMapping("/editOwnerById")
    @Transactional
    public DataGridView editOwnerById(OwnerVO ownerVO){
        //判断未入驻业主信息是否存在
        Owner owner = ownerService.getById(ownerVO.getOwnerId());
        if (owner == null){
            return new DataGridView(1,"您想要编辑的未入驻业主信息不存在","");
        }
        //判断编辑信息是否为空
        if (ownerVO.getCommunityId()==null&&ownerVO.getCommunityName()==null&&ownerVO.getOwnerAddress()==null&&ownerVO.getOwnerAge()==null&&
            ownerVO.getOwnerName()==null&&ownerVO.getOwnerPhone()==null){
            return new DataGridView(1,"您所输入的编辑信息不能为空","");
        }
        try {
            ownerService.updateById(ownerVO);
            return new DataGridView(0,"修改成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGridView(-1,"修改失败",null);
        }
    }
}

